import Vue from 'vue'
import Vuex from 'vuex'
import Mall from '@/dll/mall'
import Me from '@/dll/me'
// import Router from './router'
import { globalData } from './config/globalData'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    goodsNum: 0,
    defaultAddress: { // 用户默认地址
      code: '',
      arr: [],
      info: ''
    },
    flag: false, // 判断是否重庆电信  false: 不是 true:是
    isShowSpecs: false // 是否显示详情页的规格弹框  false: 不是 true:是
  },
  getters: {
    goodsNum: state => {
      return state.goodsNum
    },
    defaultAddress: state => {
      return state.defaultAddress
    },
    flag: state => {
      return state.flag
    },
    isShowSpecs: state => {
      return state.isShowSpecs
    }
  },
  mutations: {
    SET_GOODS_NUM (state, num) {
      state.goodsNum = num
    },
    logout () {
      sessionStorage.accesstoken = null
      globalData.accesstoken = null
      localStorage.removeItem('userInfo')
      localStorage.removeItem('accesstoken')
    },

    // 获取默认地址
    getDefaultAddress (state, address) {
      state.defaultAddress = address
    },

    // 是否重庆电信
    setFlag (state, { flag, callback }) {
      state.flag = flag
      if (callback) {
        callback()
      }
    },

    setShowSpecs (state, isShowSpecs = false) {
      state.isShowSpecs = isShowSpecs
    }
  },
  actions: {
    async GET_GOODS_NUM ({ commit }) {
      let res = await Mall.getGoodsNum()
      if (res.code == 0) {
        commit('SET_GOODS_NUM', parseInt(res.data || 0))
      }
    }
  }
})
